package com.gg.user.gguser.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgNewsDaoMapper;
import com.gg.user.gguser.dataobject.GgNewsDataobject;
import com.gg.user.gguser.dataobject.GgUserDataobject;
import com.gg.user.gguser.service.GgNewsService;
import com.gg.user.gguser.service.GgUserGetAllService;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
public class GgNewsServiceImpl implements GgNewsService {

    @Autowired
    private GgNewsDaoMapper ggNewsDaoMapper;

    @Autowired
    private GgUserGetAllService ggUserGetAllService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object getNewsSelect(HttpServletResponse response, HttpServletRequest request,Integer page,Integer pageNumber) throws Exception{
        try {
//            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
//            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); //登陆用户id
            Integer newsCount = ggNewsDaoMapper.getNewsSelectCount();
            List<GgNewsDataobject> getNewsSelectList = ggNewsDaoMapper.getNewsSelect((page-1) * pageNumber ,pageNumber);
            return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(newsCount/pageNumber),pageNumber,newsCount,getNewsSelectList),"");
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚
            return return_format.return_format_error(100003,"服务器出问题，请稍后重试！");
        }
    }

    /**
     * 单个查询
     */
    @Override
    public Object getNewSelectItems(HttpServletResponse response, HttpServletRequest request, Integer itemsId) throws Exception {
        try {
            return return_format.return_format_All(0 ,ggNewsDaoMapper.getNewSelectItems(itemsId),"");
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务器出问题，请稍后重试！");
        }

    }

    @Override
    public Object addNewSelectItems(HttpServletResponse response, HttpServletRequest request, String range, String count,String introduction,String title,String ggType) throws Exception {
        try {
            if(count == null || count == "" || range == null || range == "" || title == null || title == "") {
                return return_format.return_format_error(100003,"内容不能为空！");
            }else {
                // 发送消息  查询这个人下面的人都有那些

                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                //登陆用户id
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                // 登陆人的等级
                Integer ggGrade= JSONObject.parseObject(cookiceUser).getInteger("ggGrade");
                Map userAllItems = ggUserGetAllService.getUserStaff(ggGrade,ggIdUser,1,1000000);
                // 判断是不是 false
                if( -1 == Integer.valueOf(String.valueOf(userAllItems.get("page")))){
                    return return_format.return_format_error(100003,"等级不够提升等级！");
                }else {
                    List<GgUserDataobject> userList = (List<GgUserDataobject>) userAllItems.get("data");
                    GgNewsDataobject ggNewsDataobject= new GgNewsDataobject();
                    ggNewsDataobject.setGgNRange(range);
                    ggNewsDataobject.setGgNContent(count);
                    ggNewsDataobject.setGgNIntroduction(introduction);
                    ggNewsDataobject.setGgNTitle(title);
                    ggNewsDataobject.setGgNType(ggType);
                    ggNewsDataobject.setGgNSubmitter(ggIdUser);
                    //ggNewsDaoMapper.addNewSelectItems(range,count,introduction,title,ggType,ggIdUser);
                    ggNewsDaoMapper.addNewSelectItems(ggNewsDataobject);
                    //获取到全部的用户id然后插入数据库
                    for (int i = 0;i < userList.size();i++){
                        String getGgId = userList.get(i).getGgId(); // 用户id
                        ggNewsDaoMapper.installNewItemsRead(ggNewsDataobject.getGgNId(),getGgId);
                    }
                    return return_format.return_format_All(0,"","");
                }
            }
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务器出问题，请稍后重试！");
        }
    }

    /**
     * 删除
     * @param response
     * @param request
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Object delectNewSelectItems(HttpServletResponse response, HttpServletRequest request, Integer id) throws Exception {
        try {
            // 发送全部
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            //登陆用户id
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
            Map<Object,Object> items = ggNewsDaoMapper.getNewSelectItems(id);
            String tUserId = items.get("ggNSubmitter").toString();
            if(tUserId.equals(ggIdUser)){
                ggNewsDaoMapper.delectNewSelectItems(ggIdUser,id);
                return return_format.return_format_All(0,"","");
            }else {
                return return_format.return_format_error(100003,"不是你的东西不能删除的！");
            }
        }catch (Exception e){
            return return_format.return_format_error(100003,"服务器出问题，请稍后重试！");
        }
    }


}
